{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "92955dc3",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3a557c2c",
   "metadata": {},
   "source": [
    "# 逻辑运算"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fae2600a",
   "metadata": {},
   "source": [
    "## 普通运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "e28fcaf8",
   "metadata": {},
   "outputs": [],
   "source": [
    "stock_change = np.random.normal(0,1,(8,10))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "887a483e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 6.92282730e-01,  9.09639200e-02,  2.06447171e-01,\n",
       "        -9.19900969e-01, -1.63482667e-01, -2.83371445e-01,\n",
       "         1.87180423e-01,  1.12520691e+00, -2.52416667e-01,\n",
       "         4.70260006e-01],\n",
       "       [-6.92036307e-01, -6.88433309e-01, -5.08361613e-01,\n",
       "         4.73600450e-01, -1.55053103e-01,  1.27292767e-01,\n",
       "         1.89586668e-02,  1.43614851e+00, -1.26664013e+00,\n",
       "        -6.16989730e-01],\n",
       "       [ 8.13674570e-01,  1.01041568e+00,  1.48997111e+00,\n",
       "         2.90865141e-01, -3.70023798e-03, -8.64928389e-01,\n",
       "        -1.64580664e+00, -7.57199983e-01, -2.74732803e-01,\n",
       "        -1.18092563e+00],\n",
       "       [-3.42188195e-01, -1.38159103e+00,  5.62790829e-01,\n",
       "         9.09334651e-01,  1.21493664e-01, -6.20943164e-01,\n",
       "         1.20006670e+00,  1.48225048e+00, -1.10472006e+00,\n",
       "        -1.67788283e+00],\n",
       "       [ 1.36460349e+00,  2.16051809e+00,  2.24465842e-01,\n",
       "         1.48149565e+00, -7.90055352e-01,  2.14334477e-03,\n",
       "         1.00681827e+00,  4.65805372e-01, -1.48065324e-01,\n",
       "         9.12196980e-01],\n",
       "       [-9.73430852e-01, -4.83262325e-01,  1.78350529e-01,\n",
       "         1.13035655e+00, -4.45909101e-02,  4.78163213e-01,\n",
       "        -9.04117862e-01, -5.01380710e-01, -3.15628329e-01,\n",
       "        -1.17311181e+00],\n",
       "       [-4.10464010e-01,  5.42360464e-01,  4.12520225e-01,\n",
       "        -1.06544431e+00, -8.64500285e-01,  2.45998030e-01,\n",
       "         5.01263381e-01, -6.88702225e-01,  1.61080978e+00,\n",
       "         1.55664890e+00],\n",
       "       [ 8.75760547e-01, -3.41473881e-01,  4.08794397e-01,\n",
       "         1.51944963e+00, -6.79947639e-02, -4.79766813e-01,\n",
       "         7.34830767e-01, -1.09570347e+00, -1.79061491e+00,\n",
       "        -1.28167431e+00]])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stock_change"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "26d86e85",
   "metadata": {},
   "outputs": [],
   "source": [
    "stock_c = stock_change[0:5,0:5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "5a51bd66",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0.69228273,  0.09096392,  0.20644717, -0.91990097, -0.16348267],\n",
       "       [-0.69203631, -0.68843331, -0.50836161,  0.47360045, -0.1550531 ],\n",
       "       [ 0.81367457,  1.01041568,  1.48997111,  0.29086514, -0.00370024],\n",
       "       [-0.3421882 , -1.38159103,  0.56279083,  0.90933465,  0.12149366],\n",
       "       [ 1.36460349,  2.16051809,  0.22446584,  1.48149565, -0.79005535]])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stock_c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "87801772",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[False, False, False, False, False],\n",
       "       [False, False, False, False, False],\n",
       "       [False,  True,  True, False, False],\n",
       "       [False, False, False, False, False],\n",
       "       [ True,  True, False,  True, False]])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stock_c > 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "8d6292a9",
   "metadata": {},
   "outputs": [],
   "source": [
    "stock_c[stock_c > 1] = 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "09b457a9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0.69228273,  0.09096392,  0.20644717, -0.91990097, -0.16348267],\n",
       "       [-0.69203631, -0.68843331, -0.50836161,  0.47360045, -0.1550531 ],\n",
       "       [ 0.81367457,  2.        ,  2.        ,  0.29086514, -0.00370024],\n",
       "       [-0.3421882 , -1.38159103,  0.56279083,  0.90933465,  0.12149366],\n",
       "       [ 2.        ,  2.        ,  0.22446584,  2.        , -0.79005535]])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stock_c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "29903278",
   "metadata": {},
   "outputs": [],
   "source": [
    "test_a = [1,2,3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "f5e49874",
   "metadata": {},
   "outputs": [],
   "source": [
    "# test_a >2 # 列表里面无法进行运算"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1278346d",
   "metadata": {},
   "source": [
    "## 通用运算符"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "315cf292",
   "metadata": {},
   "outputs": [],
   "source": [
    "stock_d = stock_change[0:2,0:5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "d156511a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0.69228273,  0.09096392,  0.20644717, -0.91990097, -0.16348267],\n",
       "       [-0.69203631, -0.68843331, -0.50836161,  0.47360045, -0.1550531 ]])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stock_d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "2b56b3ee",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.all(stock_d>0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "767c01b5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.any(stock_d>0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "0f4c0a7d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 0, 0, 0, 0],\n",
       "       [0, 0, 0, 0, 0]])"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.where(stock_d>1,1,0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "de7312fc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 1, 1, 0, 1],\n",
       "       [0, 0, 0, 1, 1]])"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    " np.where(np.logical_and(stock_d>-0.5,stock_d<0.5),1,0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "98a47d3b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[2, 2, 2, 0, 0],\n",
       "       [0, 0, 0, 2, 0]])"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.where(np.logical_or(stock_d>0,stock_d<-1),2,0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "6fcd2c80",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1.12520691, 1.43614851, 2.        , 1.48225048, 2.        ,\n",
       "       1.13035655, 1.61080978, 1.51944963])"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stock_change.max(axis=1) # 行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "0813660a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2.        , 2.        , 2.        , 2.        , 0.12149366,\n",
       "       0.47816321, 1.2000667 , 1.48225048, 1.61080978, 1.5566489 ])"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stock_change.max(axis=0) # 列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "d54937a2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0.69228273,  0.09096392,  0.20644717, -0.91990097, -0.16348267],\n",
       "       [-0.69203631, -0.68843331, -0.50836161,  0.47360045, -0.1550531 ],\n",
       "       [ 0.81367457,  2.        ,  2.        ,  0.29086514, -0.00370024],\n",
       "       [-0.3421882 , -1.38159103,  0.56279083,  0.90933465,  0.12149366],\n",
       "       [ 2.        ,  2.        ,  0.22446584,  2.        , -0.79005535]])"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stock_c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "b0ea0d6e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 3, 1, 3, 0], dtype=int64)"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stock_c.argmax(axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "2aa8e1c0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([4, 2, 2, 4, 3], dtype=int64)"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stock_c.argmax(axis=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "28dc61f2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 3, 1, 0, 4], dtype=int64)"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stock_c.argmin(axis=0)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f60b70e1",
   "metadata": {},
   "source": [
    "# 数组运算"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "224cb401",
   "metadata": {},
   "source": [
    "## 简单运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "14e0345d",
   "metadata": {},
   "outputs": [],
   "source": [
    "arr = np.array([1,2,3,4,5])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "a9188a76",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 4, 5])"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "ec2de23b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2, 3, 4, 5, 6])"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr + 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "24cdec50",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.5, 1. , 1.5, 2. , 2.5])"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr / 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "d31d548c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([10, 20, 30, 40, 50])"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr * 10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "54160575",
   "metadata": {},
   "outputs": [],
   "source": [
    "arr1 = np.array([[1,2,3,4,1],[5,6,7,8,1]])\n",
    "arr2 = np.array([[1,23,41],[21,3,3]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "17658dd4",
   "metadata": {},
   "outputs": [],
   "source": [
    "# arr1+arr2 不可以运算"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "92781199",
   "metadata": {},
   "source": [
    "## 广播机制"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "5145a2d1",
   "metadata": {},
   "outputs": [],
   "source": [
    "arr1 = np.array([[1,2,3,4,1,1],[1,2,3,5,7,2]])\n",
    "arr2 = np.array([[1],[2]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "9664f98c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[2, 3, 4, 5, 2, 2],\n",
       "       [3, 4, 5, 7, 9, 4]])"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1+arr2"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c6ad4fdd",
   "metadata": {},
   "source": [
    "## 矩阵运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "85221397",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = np.array([[80,86],\n",
    "             [82,86],\n",
    "             [80,90],\n",
    "             [83,91],\n",
    "             [79,99],\n",
    "             [90,91],\n",
    "             [83,98]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "0abb8d5c",
   "metadata": {},
   "outputs": [],
   "source": [
    "b = np.array([[0.7],[0.3]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "2f9b2aac",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[81.8],\n",
       "       [83.2],\n",
       "       [83. ],\n",
       "       [85.4],\n",
       "       [85. ],\n",
       "       [90.3],\n",
       "       [87.5]])"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.matmul(a,b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "e6c24ca8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[81.8],\n",
       "       [83.2],\n",
       "       [83. ],\n",
       "       [85.4],\n",
       "       [85. ],\n",
       "       [90.3],\n",
       "       [87.5]])"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.dot(a,b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "4d2f9899",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[800, 860],\n",
       "       [820, 860],\n",
       "       [800, 900],\n",
       "       [830, 910],\n",
       "       [790, 990],\n",
       "       [900, 910],\n",
       "       [830, 980]])"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.dot(a,10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "e43d6260",
   "metadata": {},
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "module 'numpy' has no attribute 'matmu'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "\u001b[1;32m~\\AppData\\Local\\Temp\\ipykernel_45620\\925439926.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mnp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mmatmu\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0ma\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m10\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;32m~\\AppData\\Roaming\\Python\\Python37\\site-packages\\numpy\\__init__.py\u001b[0m in \u001b[0;36m__getattr__\u001b[1;34m(attr)\u001b[0m\n\u001b[0;32m    312\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    313\u001b[0m             raise AttributeError(\"module {!r} has no attribute \"\n\u001b[1;32m--> 314\u001b[1;33m                                  \"{!r}\".format(__name__, attr))\n\u001b[0m\u001b[0;32m    315\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    316\u001b[0m         \u001b[1;32mdef\u001b[0m \u001b[0m__dir__\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mAttributeError\u001b[0m: module 'numpy' has no attribute 'matmu'"
     ]
    }
   ],
   "source": [
    "# np.matmu(a,10) # matmul不支持矩阵和数字相乘"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dfa74bc1",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
